package com.itheima.health.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.health.POJO.CheckItem;
import com.itheima.health.common.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.CheckItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/checkitem")
public class CheckItemController {
    @Autowired
    private CheckItemService checkItemService;
    @Autowired
    HttpServletRequest request;

    /**
     * @description 新增检查项
     * @date 2021/11/23 14:44
     * @param checkItem:
     * @return com.itheima.health.entity.Result
     */
    @PostMapping("/add.do")
    public Result addCheckItem(@RequestBody CheckItem checkItem){
        //1.前端页面中编码和名字为必填项，查询所有检查项中是否有相同的编码和名字
        LambdaQueryWrapper<CheckItem> wrapper=new LambdaQueryWrapper<>();
        //判断有无相同编码
        wrapper.eq(CheckItem::getCode,checkItem.getCode()).or().eq(CheckItem::getName,checkItem.getName());
        CheckItem one = checkItemService.getOne(wrapper);
        //编码相同，返回错误
        if(one!=null){
            return new Result(false,"存在相同的编码或名字",one);
        }
        //fixme 出现参数匹配异常，当传参id为null时会自动生成一个Long类型
        //todo 因为mybatis-plus中id的自动生成使用了雪花算法，在yml配置文件中将id生成策略改为数据库默认递增
        //2.调用service方法
        boolean save = checkItemService.save(checkItem);
        if(!save){
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL,null);
        }
        return new Result(true,MessageConstant.ADD_CHECKITEM_SUCCESS,checkItem);
    }

    /**
     * @description 查询所有检查项
     * @date 2021/11/23 15:42
     * @return com.itheima.health.entity.Result
     */
    @GetMapping("/findAll.do")
    public Result findAllItem(){
        List<CheckItem> checkItems = checkItemService.list();
        return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItems);
    }

    /**
     * @description 根据ID查询检查项,数据回显
     * @date 2021/11/23 15:49
     * @param id:
     * @return com.itheima.health.entity.Result
     */
    @GetMapping("/findById.do")
    public Result findById(Integer id){
        LambdaQueryWrapper<CheckItem> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(CheckItem::getId,id);
        CheckItem one = checkItemService.getOne(wrapper);
        if(one==null){
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }
        return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,one);
    }

    /**
     * @description 分页查询-条件查询
     * @date 2021/11/23 15:51
     * @param pageBean:
     * @return com.itheima.health.entity.Result
     */
    @PostMapping("/findPage.do")
    public PageResult findByPage(@RequestBody QueryPageBean pageBean){
        //0.加入分页查询插件
        Integer currentPage = pageBean.getCurrentPage();
        Integer pageSize = pageBean.getPageSize();
        String queryString = pageBean.getQueryString();
        //2.判断页码和每页记录数要大于0
        if(currentPage<=0 || pageSize<=0){
            return null;
        }
        //plus分页
        IPage<CheckItem> pageInfo=new Page<>(currentPage,pageSize);
        LambdaQueryWrapper<CheckItem> wrapper=new LambdaQueryWrapper<>();
        if(queryString!=null) {
            wrapper.like(CheckItem::getCode, queryString).or().like(CheckItem::getName, queryString);
        }
        IPage<CheckItem> page = checkItemService.page(pageInfo, wrapper);
        return new PageResult(page.getTotal(), page.getRecords());
    }

    /**
     * @description 编辑检查项
     * @date 2021/11/23 16:49
     * @param checkItem:
     * @return com.itheima.health.entity.Result
     */
    @PostMapping("/edit.do")
    public Result updateCheckItem(@RequestBody CheckItem checkItem){
        //1.调用service
        boolean b = checkItemService.updateById(checkItem);
        if(!b){
            return new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL,null);
        }
        return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS,checkItem);
    }

    /**
     * @description 删除检查项
     * @date 2021/11/23 16:54
     * @param id:
     * @return com.itheima.health.entity.Result
     */
    @GetMapping("/delete.do")
    public Result delete(Integer id){
        boolean b = checkItemService.removeById(id);
        if(!b){
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL,null);
        }
        return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS,id);
    }
}
